package com.restaurant.application.root.commonui.data;

import com.google.common.collect.Lists;
import com.nebutown.generation.Convert;
import com.nebutown.generation.PickableBean;

import java.util.List;
import java.util.stream.Collectors;

public class EditorValue implements PickableBean {

    public static final String FN_NAME = "name";
    public static final String FN_VALUE = "value";
    public static final String FN_VALUESELECTION = "valueSelection";
    public static final String FN_BUTTON = "button";
    public static final String FN_DYNAMIC = "dynamic";

    public static final List<String> FIELD_NAMES = Lists.newArrayList(
            FN_NAME,
            FN_VALUE,
            FN_VALUESELECTION,
            FN_BUTTON,
            FN_DYNAMIC);

    private String name;
    private String value;
    private List<String> valueSelection;
    private String button;
    private DynamicSearchField dynamic;

    public EditorValue(String name) {
        setName(name);
    }

    public EditorValue() {
    }

    public EditorValue duplicate() {
        EditorValue result = new EditorValue();
        result.name = this.name;
        result.value = this.value;
        result.valueSelection = this.valueSelection == null ? null : this.valueSelection.stream().map(e3 -> e3).collect(Collectors.toList());
        result.button = this.button;
        result.dynamic = this.dynamic == null ? null : this.dynamic.duplicate();
        return result;
    }

    @Override
    public String toString() {
        return Convert.toJson(this);
    }

    private void cleanSource() {
        valueSelection = null;
        button = null;
        dynamic = null;
    }

    public List<String> getValueSelection() {
        return valueSelection;
    }

    public void setValueSelection(List<String> value) {
        cleanSource();
        this.valueSelection = value;
    }

    public EditorValue withValueSelection(List<String> value) {
        setValueSelection(value);
        return this;
    }

    public EditorValue withValueSelection(List<String> value, List<String> defaultValue) {
        setValueSelection(value == null ? defaultValue : value);
        return this;
    }

    public EditorValue addValueSelection(String value) {
        List<String> old_valueSelection = this.valueSelection;
        cleanSource();
        this.valueSelection = old_valueSelection;
        if (valueSelection == null) {
            valueSelection = Lists.newArrayList();
        }
        this.valueSelection.add(value);
        return this;
    }

    public String getButton() {
        return button;
    }

    public void setButton(String value) {
        cleanSource();
        this.button = value;
    }

    public EditorValue withButton(String value) {
        setButton(value);
        return this;
    }

    public EditorValue withButton(String value, String defaultValue) {
        setButton(value == null ? defaultValue : value);
        return this;
    }

    public DynamicSearchField getDynamic() {
        return dynamic;
    }

    public void setDynamic(DynamicSearchField value) {
        cleanSource();
        this.dynamic = value;
    }

    public EditorValue withDynamic(DynamicSearchField value) {
        setDynamic(value);
        return this;
    }

    public EditorValue withDynamic(DynamicSearchField value, DynamicSearchField defaultValue) {
        setDynamic(value == null ? defaultValue : value);
        return this;
    }

    private void cleanValue() {
        value = null;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        cleanValue();
        this.value = value;
    }

    public EditorValue withValue(String value) {
        setValue(value);
        return this;
    }

    public EditorValue withValue(String value, String defaultValue) {
        setValue(value == null ? defaultValue : value);
        return this;
    }

    public String getName() {
        return name;
    }

    public void setName(String value) {
        this.name = value;
    }

    public EditorValue withName(String value) {
        setName(value);
        return this;
    }

    public EditorValue withName(String value, String defaultValue) {
        setName(value == null ? defaultValue : value);
        return this;
    }

    public Object getValueOf(String name) {
        switch (name) {
            case "name":
                return getName();
            case "value":
                return getValue();
            case "valueSelection":
                return getValueSelection();
            case "button":
                return getButton();
            case "dynamic":
                return getDynamic();
        }
        return null;
    }

    public boolean hasField(String name) {
        return FIELD_NAMES.contains(name);
    }

    @Override
    public int hashCode() {
        return signature().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof EditorValue)) return false;
        return signature().equals(((EditorValue) o).signature());
    }

    public String signature() {
        throw new RuntimeException("Bean EditorValue has no signature defined, no hash code or equals possible.");
    }
}
